package com.czh.zhiwen.scoring;

import com.czh.zhiwen.common.ErrorCode;
import com.czh.zhiwen.exception.BusinessException;
import com.czh.zhiwen.model.entity.App;
import com.czh.zhiwen.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 评分策略执行器
 */
@Service
public class ScoringStrategyExector {

    @Resource
    private List<ScoringStrategy> scoringStrategyList;

    /**
     * 执行评分策略
     * @param choices
     * @param app
     * @return
     * @throws Exception
     */
    public UserAnswer doScore(List<String> choices, App app) throws Exception {
        Integer appType = app.getAppType();
        Integer scoringStrategy = app.getScoringStrategy();
        if(appType == null || scoringStrategy == null){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有误，未找到匹配策略");
        }
        if(scoringStrategyList == null || scoringStrategyList.isEmpty()){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"评分策略列表为空");
        }
        for (ScoringStrategy Strategy : scoringStrategyList) {
            if(Strategy.getClass().isAnnotationPresent(ScoringStrategyConfig.class)){
                ScoringStrategyConfig annotation = Strategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if(annotation.appType() == appType && annotation.scoringStrategy() == scoringStrategy)
                    return Strategy.doScore(choices, app);
            }
        }
        throw  new BusinessException(ErrorCode.SYSTEM_ERROR,"应用配置有误，未找到匹配策略");
    }
}
